package com.fortune.service.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.fortune.dal.dao.WorkDayDAO;
import com.fortune.dal.model.WorkDay;
import com.fortune.service.WorkDayService;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 工作日查询
 */
@Service
public class WorkDayServiceImpl implements WorkDayService {

    protected final Logger logger = LoggerFactory.getLogger(WorkDayService.class);

    private static Map<String, WorkDay> workDayCache = Maps.newConcurrentMap();

    @Autowired
    private WorkDayDAO workDayDAO;

    @Override
    public WorkDay queryByWorkDate(String workDate) throws BusinessException {
        WorkDay workDay = workDayCache.get(workDate);
        if (workDay == null) {
            workDay = workDayDAO.queryByWorkDate(workDate);
            workDayCache.put(workDate, workDay);
        }
        return workDay;
    }

    @Override
    public List<WorkDay> queryByModel(WorkDay workDay) throws BusinessException {
        return workDayDAO.queryByModel(workDay);
    }

    @Override
    public boolean isWorkDay(String workDate) throws BusinessException {
        WorkDay workDay = queryByWorkDate(workDate);
        if (workDay == null) {
            throw new BusinessException("【" + workDate + "】数据格式不正确");
        }
        return WorkDayService.work_day.equals(workDay.getWorkFlag());
    }

    @Override
    public WorkDay getNextWorkDay(String natureDay) throws BusinessException {
        WorkDay workDay = null;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(natureDay));
            do {
                c.add(Calendar.DATE, 1);
                natureDay = DateFormatUtils.yyyyMMdd.format(c.getTime());
            } while (!isWorkDay(natureDay));
            workDay = queryByWorkDate(natureDay);
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
        return workDay;
    }

    @Override
    public WorkDay getNextFirstWorkDay(String workdate) throws BusinessException {
        WorkDay workDay = null;
        String date = "";
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateFormatUtils.yyyyMMdd.parse(workdate));
            do {
                calendar.add(Calendar.DATE, 1);
                date = DateFormatUtils.yyyyMMdd.format(calendar.getTime());
            } while (isWorkDay(date));
            date = getNextWorkDay(date).getWorkDate();
            workDay = queryByWorkDate(date);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return workDay;
    }

    @Override
    public String getLastWorkDay(String naturalDay) throws BusinessException {
        String date = "";
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateFormatUtils.yyyyMMdd.parse(naturalDay));
            do {
                calendar.add(Calendar.DATE, -1);
                date = DateFormatUtils.yyyyMMdd.format(calendar.getTime());
            } while (isWorkDay(date));
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return date;
    }

    @Override
    public String get7DayLastWorkDay(String naturalDay) throws BusinessException {
        String date = "";
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateFormatUtils.yyyyMMdd.parse(naturalDay));
            calendar.add(Calendar.DATE, -7);
            while (!isWorkDay(date)) {
                calendar.add(Calendar.DATE, -1);
                date = DateFormatUtils.yyyyMMdd.format(calendar.getTime());
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return date;
    }

    @Override
    public String get1MonthLastWorkDay(String naturalDay) throws BusinessException {
        return null;
    }

    @Override
    public String get3MonthLastWorkDay(String naturalDay) throws BusinessException {
        return null;
    }

    @Override
    public String get6MonthLastWorkDay(String naturalDay) throws BusinessException {
        return null;
    }

    @Override
    public String get1YearLastWorkDay(String naturalDay) throws BusinessException {
        return null;
    }
}